<?php
  /*
  This piece formats the background and connects to the database
  */
echo "<center><img src='images/Title.png'></center><br />";
echo '<body style="background-color:black">';

  $conn = mysql_connect('studentdb.gl.umbc.edu', 'chrimo1', 'chrimo1');
  if (!$conn) {
    die('Could not connect');     
      }
  $db_selected = mysql_select_db('chrimo1', $conn);
  if (!$db_selected) {
      die ('Can\'t use chrimo1');
      }
  /*
  This piece gets all of the fields from the html, and allow sing quotes
  */
$name = mysql_real_escape_string($_POST["name"]);
$shipping = mysql_real_escape_string($_POST["shipping"]);
$user = mysql_real_escape_string($_POST["username"]);
$pass = mysql_real_escape_string($_POST["password"]);
$email = mysql_real_escape_string($_POST["email"]);



 
   if($user == "" || $pass == "" || $name == "" || $shipping == "" || $email == ""){
      echo "<center><font color='gold' size=48><br />ERROR!<br />You must fill in all the fields. Click Below to try again</font></center>";
     echo "<center><a href='register.html'><font size=48>Try Again</font></a></center>";    //fail if they didnt enter in all the fields
     
   }
  /*
  if they did fill out all the fields, either tell them their username is taken, or register successfully
  */ 
   else{
  
      $sql = "SELECT username FROM customerInfo WHERE username = '$user'";
      $isUsernameValid = mysql_query($sql, $conn);
      $info = mysql_fetch_array($isUsernameValid, $conn);
      $u = $info['username'];
     /*If username is taken*/
      if($u != ""){
	 echo "<center><font color='gold' size=48>ERROR!<br />This username already exists, please try again</font></center>";
	 echo "<center><a href='register.html'><font size=48>Try Again</font></a>";
      }
     /*if username not taken*/
      else{
	 echo $u;
	 $newSql = "INSERT INTO customerInfo (name, shippingAddress, username, passwd, email) VALUES ('$name', '$shipping', '$user', '$pass', '$email')";
	 $register = mysql_query($newSql, $conn);
	 if(!$register){
      echo "Registration Failed";     //a little just in case
	    die(mysql_error());
	 }
	 else{
	     echo "<center><br /> <img src='images/registration_complete.PNG'></center><br />";
	    echo "<br /><center><a href='login.html'><font size=48><img src='images/Continue.PNG'></font></a></center></center>";
	 }
      }
   }  

?>